/*
 * JCaptcha, the open source java framework for captcha definition and integration
 * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
 * See the LICENSE.txt file distributed with this package.
 */

/*
 * jcaptcha, the open source java framework for captcha definition and integration
 * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
 * See the LICENSE.txt file distributed with this package.
 */
package org.ponly.cta4j.drawing.color;

import java.awt.Color;
import java.security.SecureRandom;

/**
 * 一个从预定义颜色列表中随意获取的颜色生成器.
 *
 * @author Benoit Doumas
 * @author Chrsitian Blavier
 * @since 1.0
 */
public class RandomColorGenerator implements ColorGenerator {
    /**
     * Use for random color selection.
     */
    private final SecureRandom random = new SecureRandom();

    /**
     * List of colors that can be selected.
     */
    private Color[] colors;

    /**
     * Constructor that take an array of Color.
     *
     * @param colors the array of color
     */
    public RandomColorGenerator(final Color... colors) {
        if (null == colors || 1 > colors.length) {
            throw new IllegalStateException("Color list cannot be empty");
        }
        for (final Color color : colors) {
            if (null == color) {
                throw new IllegalStateException("One or several color is null");
            }
        }
        this.colors = colors;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Color getColor() {
        final int index = colors.length < 2 ? 0 : random.nextInt(this.colors.length);
        return colors[index];
    }
}
